<?php
// Скрипт для перевода из различных систем счисления
// Двоичная, восьмеричная, десятеричная, шестнадцатеричная
// Автор: Серышев Михаил
// 11.09.2012
// Версия 1.0

if (isset($_POST['convert'])) {
    $from = (int)$_POST['from'];
    $to   = (int)$_POST['to'];
    if ($from <= 10) {
        $numb = (int)$_POST['numb'];
    } else {
        $numb = (string)$_POST['numb'];
    }
    $answer = base_convert($numb, $from, $to);
} else {
    $numb = '1010011010';
    $from = 2;
    $to = 16;
}

function selected($s, $from) {
    if ($s == $from) {
        return 'selected';
    } else {
        return '';
    }
}

echo '

<!DOCTYPE html>
<html>
<head>
    <title>BydloCalc</title>
    <meta charset="UTF-8">
<style>
html, body {
    font-family: Arial;
    word-wrap: break-word;
}
.calc_div {
    width: 176px;
    height: 220px;
    background-color: #eee;
    color: #222;
    border-radius: 4px;
    border: 1px solid #666;
    text-align: center;
}
input {
    margin: 4px;
    width: 168px;
    border: 1px solid #666;
    border-radius: 4px;
    background-color: #fff;
}
select {
    margin: 0px 4px;
    width: 168px;
    border: 1px solid #666;
    border-radius: 4px;
    background-color: #fff;
}
sub {
    color: #00f;
}
.answer {
    margin: 0px 4px;
    width: 164px;
    border: 1px solid #666;
    border-radius: 4px;
    background-color: #fff;
    text-transform: uppercase;
}
</style>
<!-- BydloConverer 1.0 by Seryshev Mihail -->
</head>
<body>

<div class="calc_div">
<form action="conv.php" method="post">
    <input type="text" value="'.$numb.'" name="numb" placeholder="Число" required pattern="[0-9a-fA-F\.\,]{1,64}" style="width: 164px;">
    <br>
    
    <select type="text"  name="from" required>
        <option value="2" '.selected(2, $from).'>2</option>
        <option value="8" '.selected(8, $from).'>8</option>
        <option value="10" '.selected(10, $from).'>10</option>
        <option value="16" '.selected(16, $from).'>16</option>
    </select>
    <br>
    <select type="text" name="to" required>
        <option value="2" '.selected(16, $to).'>2</option>
        <option value="8" '.selected(16, $to).'>8</option>
        <option value="10" '.selected(16, $to).'>10</option>
        <option value="16" '.selected(16, $to).'>16</option>
    </select>
    <br>
    <input type="submit" name="convert" value="Перевести">
</form>';

if (isset($answer)) {
    echo '<div class="answer"><span>'.$numb.'</span><sub>'.$from.'</sub> <span>=</span> <span>'.$answer.'</span><sub>'.$to.'</sub></div>';
}

echo '
</div>

</body>
</html>
';
?>
